import { //核心
    React, Enhance, Controller, Component, $, EventEmitter,
    //
    tools, classnames,
    // 组件
    Table, TabPage, Lang, Dialog, Tree,
    Form, Page, Upload, Icon, Grid, Http,
    BoxWidget, Input, Button, Prompt, Link, Loaders, Editor, Resource, Directive, Lock, Fix
} from 'admin';

import Cars from './w-cars';
import Fittingtype from './w-fittingtype';
/**
 * 配件
 */
class Fitting extends Component {

    /**
     * 仓储对象缓存
     */
    static cache = null
    state = {
        edit: {}
    }

    _lang(name){
        return `ysw.fitting.${name}`;
    }
    /**
     * componentDidMount()在挂载结束之后马上被调用。需要DOM节点的初始化操作应该放在这里。
     */
    componentDidMount() {
        // this.handlerLoadCache();
    }

    render() {
        let {edit} = this.state;
        console.log(edit)
        return (
            <div>
                <Http ref="http"></Http>
                <Directive name="Modal">
                    <Dialog ref="addDialog" title={this._lang("add")}>
                        <div data-fix="elem">
                            <Form
                                action={`${window.SERVER_URL}/ysw/fitting/do_add/`}
                                onSuccess={this.handlerAddSuccess}
                                onError={this.handlerAddError}
                                rules={{
                                    "code": "required",
                                    "name": "required",
                                    "origin": "required",
                                    "serie_id": "required",
                                    "type_id": "required",
                                    "w_price": "price",
                                    "p_price": "price",
                                    "o_price": "price",
                                    "l_price": "price",
                                    "use": "int",
                                    "num": "int",
                                    "min": "int"
                                }}
                            ></Form>
                        </div>
                        <div data-fix="content">
                            <Input name="code" type="text" message={this._lang("code")}/>
                            <Input name="name" type="text" message={this._lang("name")}/>
                            <Input name="origin" type="text" message={this._lang("origin")}/>
                            <Input name="model" type="text" message={this._lang("model")}/>
                            <Input name="unit" type="text" message={this._lang("unit")}/>
                            <Input name="w_price" type="number" message={this._lang("w_price")} value="0"/>
                            <Input name="p_price" type="number" message={this._lang("p_price")} value="0"/>
                            <Input name="o_price" type="number" message={this._lang("o_price")} value="0"/>
                            <Input name="l_price" type="number" message={this._lang("l_price")} value="0"/>

                            <Input type="select" option={Cars.cache||[]} name="serie_id" message={this._lang("cars")} placeholder="Please select"/>
                            <Input type="select" option={Fittingtype.cache||[]} name="type_id" message={this._lang("fittingtype")} placeholder="Please select"/>

                            <Input name="use" type="number" message={this._lang("use")} value="0"/>
                            <Input name="min" type="number" message={this._lang("min")} value="0"/>
                            <Input name="pos_code" type="text" message={this._lang("pos_code")}/>
                            <Input name="content" type="text" message={this._lang("content")}/>

                            <Input name="num" type="number" message={this._lang("num")} value="0"/>
                            <Input name="locat" type="text" message={this._lang("locat")}/>
                        </div>
                        <div data-fix="buttons">
                            <Button type="submit">
                                <Lang lang="submit"></Lang>
                            </Button>
                        </div>
                    </Dialog>
                </Directive>
                <Directive name="Modal">
                    <Dialog ref="editDialog" title={this._lang("edit")}>
                        <div data-fix="elem">
                            <Form
                                action={`${window.SERVER_URL}/ysw/fitting/do_edit/`}
                                onSuccess={this.handlerEditSuccess}
                                onError={this.handlerEditError}
                                rules={{
                                    "code": "required",
                                    "name": "required",
                                    "origin": "required",
                                    "serie_id": "required",
                                    "type_id": "required",
                                    "w_price": "price",
                                    "p_price": "price",
                                    "o_price": "price",
                                    "l_price": "price",
                                    "use": "int",
                                    "num": "int",
                                    "min": "int"
                                }}
                            ></Form>
                        </div>
                        <div data-fix="content">
                            <Input name="id" type="hidden" model={edit} />
                            <Input name="code" type="text" message={this._lang("code")} model={edit}/>
                            <Input name="name" type="text" message={this._lang("name")} model={edit}/>
                            <Input name="origin" type="text" message={this._lang("origin")} model={edit}/>
                            <Input name="model" type="text" message={this._lang("model")} model={edit}/>
                            <Input name="unit" type="text" message={this._lang("unit")} model={edit}/>
                            <Input name="w_price" type="number" message={this._lang("w_price")} value="0" model={edit}/>
                            <Input name="p_price" type="number" message={this._lang("p_price")} value="0" model={edit}/>
                            <Input name="o_price" type="number" message={this._lang("o_price")} value="0" model={edit}/>
                            <Input name="l_price" type="number" message={this._lang("l_price")} value="0" model={edit}/>
                            <Input type="select" option={Cars.cache||[]} name="serie_id" placeholder="Please select" message={this._lang("cars")} model={edit}/>
                            <Input type="select" option={Fittingtype.cache||[]} name="type_id" placeholder="Please select"  message={this._lang("fittingtype")}  model={edit}/>
                            <Input name="use" type="number" message={this._lang("use")} value="0" model={edit}/>
                            <Input name="min" type="number" message={this._lang("min")} value="0" model={edit}/>
                            <Input name="pos_code" type="text" message={this._lang("pos_code")} model={edit}/>
                            <Input name="content" type="text" message={this._lang("content")} model={edit}/>

                            <Input name="num" type="number" message={this._lang("num")} value="0" model={edit}/>
                            <Input name="locat" type="text" message={this._lang("locat")} model={edit}/>
                        </div>
                        <div data-fix="buttons">
                            <Button type="submit">
                                <Lang lang="submit"></Lang>
                            </Button>
                        </div>
                    </Dialog>
                </Directive>
            </div>
        );
    }

    /**
     * 加载缓存
     */
        // handlerLoadCache = () => {
        //     if (!Fitting.cache) {
        //         this.refs.http.send(`${window.SERVER_URL}/ysw/fitting/get_fitting/`)
        //             .success((data)=>{
        //                 Storage.cache = data;
        //                 this.props.onCache && this.props.onCache()
        //             })
        //             .error();
        //     }
        // }

    handlerAddSuccess = (data) => {
        if (data) {
            this.props.onAddSuccess && this.props.onAddSuccess(data);
            this.hideAddDialog();
        }
    }

    handlerAddError = () => {
        this.props.onAddError && this.props.onAddError();
    }

    /**
     * 显示添加
     */
    showAddDialog() {
        this.refs.addDialog.show()
    }

    /**
     * 隐藏添加
     */
    hideAddDialog() {
        this.refs.addDialog.hide()
    }

    /**
     *
     * @param {*} edit
     */
    showEditDialog(edit) {
        this.setState({edit}, () => {
            this.refs.editDialog.show();
        });
    }

    hideEditDialog() {
        this.refs.editDialog.hide();
    }

    /**
     *
     */
    handlerEditSuccess = (data) => {
        if (data) {
            this.props.onEditSuccess && this.props.onEditSuccess(data);
            this.hideEditDialog();
        }
    }

    handlerEditError = () => {
        this.props.onEditError && this.props.onEditError();
    }
}

Enhance(Fitting)
export default Fitting